﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CYLScheck.UIcomm
{
    /// <summary>
    /// 自己写的  ini 文件读写类需要new 非静态类必须在UI层
    /// </summary>
    public class OperateiniConfig
    {
        public Dictionary<string, string> configData;
        string fullFileName;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_fileName">操作的文件名</param>
        public OperateiniConfig(string _fileName)
        {
            configData = new Dictionary<string, string>();
            fullFileName = Application.StartupPath + @"\" + _fileName;
            bool hasCfgFile = File.Exists(Application.StartupPath + @"\" + _fileName);
            if (hasCfgFile == false)
            {
                StreamWriter writer = new StreamWriter(File.Create(Application.StartupPath + @"\" + _fileName), UnicodeEncoding.GetEncoding("UTF-8"));
                writer.Close();
            }
            StreamReader reader = new StreamReader(Application.StartupPath + @"\" + _fileName, Encoding.GetEncoding("UTF-8"));
            string line = "";

            int indx = 0;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith(";") || string.IsNullOrEmpty(line))
                    configData.Add(";" + indx++, line);
                else
                {
                    string[] key_value = line.Split('=');
                    if (key_value.Length >= 2)
                        configData.Add(key_value[0], key_value[1]);
                    else
                        configData.Add(";" + indx++, line);
                }
            }
            reader.Close();
        }
        /// <summary>
        /// 读指定配置键读不到返回null
        /// </summary>
        /// <param name="key">配置键</param>
        /// <returns>键值</returns>
        public string get(string key)
        {
            if (configData.Count <= 0)
                return null;
            else if (configData.ContainsKey(key))
                return configData[key].ToString();

            else
                return null;
        }
        /// <summary>
        /// 修改键的内容不存在的项自动写入修改完调用save
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">新内容</param>
        public void set(string key, string value)
        {
            if (configData.ContainsKey(key))
                configData[key] = value;

            else
                configData.Add(key, value);
        }
        /// <summary>
        /// 设置完一起保存不存在的项自动写入
        /// </summary>
        public void save()
        {
            StreamWriter writer = new StreamWriter(fullFileName, false, UnicodeEncoding.GetEncoding("UTF-8"));
            IDictionaryEnumerator enu = configData.GetEnumerator();
            while (enu.MoveNext())
            {
                if (enu.Key.ToString().StartsWith(";"))
                    writer.WriteLine(enu.Value);
                else
                    writer.WriteLine(enu.Key + "=" + enu.Value);
            }
            writer.Close();
        }
    }
}
